using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Resources;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Reflection;
using Microsoft.Office.Server;
using Microsoft.Office.Server.ApplicationRegistry.Runtime;
using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;

namespace MacroView.SharePointFields
{
    public class BDCPropertyEditor : UserControl, IFieldEditor
    {
        protected SPField CurrentField;
        //protected TextBox txtApplication;
        //protected TextBox txtEntity;
        protected Label lblErrorLabel;
        protected Label lblApplication;
        protected Label lblEntity;
        protected Label lblDisplay;
        protected TextBox txtDisplayFields;
        protected TextBox txtReturnField;
        protected Label lblReturnField;
        protected DropDownList cboApplications;
        protected DropDownList cboEntities;

        public override void Focus()
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {

            base.CreateChildControls();

            cboApplications.SelectedIndexChanged += new EventHandler(cboApplications_SelectedIndexChanged);

            NamedLobSystemInstanceDictionary sysInstances = ApplicationRegistry.GetLobSystemInstances();
            if (sysInstances.Count <= 0)
            {
                txtDisplayFields.Enabled = false;
                cboApplications.Enabled = false;
                cboEntities.Enabled = false;

                txtDisplayFields.Visible = false;
                cboApplications.Visible = false;
                cboEntities.Visible = false;

                lblApplication.Visible = false;
                lblEntity.Visible = false;
                lblDisplay.Visible = false;
                txtReturnField.Visible = false;
                lblReturnField.Visible = false;

                this.lblErrorLabel.Text = "There are no Business Data Applications defined on this Server. Please contact your Administrator for assistance.";

            }
            else
            {
                this.lblErrorLabel.Text = "";

                txtDisplayFields.Enabled = true;
                cboApplications.Enabled = true;
                cboEntities.Enabled = true;

                txtDisplayFields.Visible = true;
                cboApplications.Visible = true;
                cboEntities.Visible = true;

                lblApplication.Visible = true;
                lblEntity.Visible = true;
                lblDisplay.Visible = true;

                txtReturnField.Visible = true;
                lblReturnField.Visible = true;


            }

        }

        void cboApplications_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboApplications.SelectedValue != "")
            {
                NamedLobSystemInstanceDictionary sysInstances = ApplicationRegistry.GetLobSystemInstances();
                LobSystemInstance currentInstance = sysInstances[cboApplications.SelectedValue];
                NamedEntityDictionary currentEntities = currentInstance.GetEntities();
                foreach (String name in currentEntities.Keys)
                {
                    cboEntities.Items.Add(name);
                }
            }
        }

        #region IFieldEditor Members

        public bool DisplayAsNewSection
        {
            get { return true; }
        }

        public void InitializeWithField(SPField field)
        {
            try
            {

                BDCField BDCField = field as BDCField;
                if (BDCField != null)
                {
                    cboApplications.Enabled = false;

                    NamedLobSystemInstanceDictionary sysInstances = ApplicationRegistry.GetLobSystemInstances();
                    cboApplications.Items.Clear();

                    foreach (String name in sysInstances.Keys)
                    {
                        cboApplications.Items.Add(name);
                    }

                    this.cboApplications.SelectedValue = BDCField.Application;

                    LobSystemInstance currentInstance = sysInstances[cboApplications.SelectedValue];
                    NamedEntityDictionary currentEntities = currentInstance.GetEntities();
                    foreach (String name in currentEntities.Keys)
                    {
                        cboEntities.Items.Add(name);
                    }

                    this.cboEntities.SelectedValue = BDCField.Entity;

                    cboEntities.Enabled = false;
                    if (!this.IsPostBack)
                    {
                        this.cboApplications.SelectedValue = BDCField.Application;
                        this.cboEntities.SelectedValue = BDCField.Entity;
                        this.txtDisplayFields.Text = BDCField.DisplayFields;
                        this.txtReturnField.Text = BDCField.ReturnField;
                    }
                }
                else
                {
                    if (cboApplications.SelectedValue == "")
                    {
                        NamedLobSystemInstanceDictionary sysInstances = ApplicationRegistry.GetLobSystemInstances();
                        cboApplications.Items.Clear();

                        foreach (String name in sysInstances.Keys)
                        {
                            cboApplications.Items.Add(name);
                        }

                        cboApplications.SelectedIndex = 1;

                        LobSystemInstance currentInstance = sysInstances[cboApplications.SelectedValue];
                        NamedEntityDictionary currentEntities = currentInstance.GetEntities();
                        foreach (String name in currentEntities.Keys)
                        {
                            cboEntities.Items.Add(name);
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                //throw new Exception("An error occurred getting custom properties for field " + field.Title + ". " + ex.ToString());
            }
        }

        public void OnSaveChange(SPField field, bool isNewField)
        {
            try
            {

                BDCField currentBDCField = field as BDCField;

                if (this.txtDisplayFields.Text == "")
                {
                    throw new Exception("The Display Fields property cannot be blank. Please enter a value.");
                }

                if (isNewField)
                {
                    currentBDCField.UpdateApplicationProperty(this.cboApplications.SelectedValue);
                    currentBDCField.UpdateEntityProperty(this.cboEntities.SelectedValue);
                    currentBDCField.UpdateDisplayFieldsProperty(this.txtDisplayFields.Text);
                    currentBDCField.UpdateReturnFieldProperty(this.txtReturnField.Text);
                }
                else
                {
                    currentBDCField.Application = this.cboApplications.SelectedValue;
                    currentBDCField.Entity = this.cboEntities.SelectedValue;
                    currentBDCField.DisplayFields = this.txtDisplayFields.Text;
                    currentBDCField.ReturnField = this.txtReturnField.Text;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred setting custom properties for field " + field.Title + ". " + ex.Message.ToString());
            }
        }

        #endregion
    }


}
